// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Utils from "./GameCore";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Craft extends cc.Component {

    @property({
        type: [cc.SpriteFrame],
        tooltip: '随机粒子效果列表'
    })
    particle: cc.SpriteFrame[] = []
  
    @property({
        type: cc.SpriteFrame,
        tooltip: '星星效果'
    })
    star: cc.SpriteFrame = null
  
    showCraft (pos: cc.Vec2, size: number) {
        // 三角粒子
        for (let i = 0; i < 30; ++i) {
            const node = new cc.Node('Sprite');
            const sprite = node.addComponent(cc.Sprite);

            sprite.spriteFrame = this.particle[Utils.randInt(this.particle.length)];
            node.parent = this.node;

            const a = 360 * Math.random(),
                i = 30 * Math.random() + size / 2,
                l = cc.v2(Math.sin(a * Math.PI / 180) * i, Math.cos(a * Math.PI / 180) * i);
            node.scale = .5 * Math.random() + size / 100;
            const p = .5 * Math.random();

            node.setPosition(pos);
            node.runAction(
                cc.sequence(cc.spawn(cc.moveBy(p, l),
                    cc.scaleTo(p + .5, .3),
                    cc.rotateBy(p + .5, Utils.randInt(720) - 360)),
                    cc.fadeOut(.1),
                    cc.callFunc(function () {
                        node.active = false
                    }, this))
            )
        }

        // 星光
        const node = new cc.Node('Sprite');
        const sprite = node.addComponent(cc.Sprite);

        sprite.spriteFrame = this.star;
        node.parent = this.node;

        node.setPosition(pos)
        node.scale = 0
        node.angle = Utils.randInt(360)
        node.runAction(cc.sequence(cc.spawn(cc.scaleTo(.2, size / 150), cc.fadeOut(1)), cc.callFunc(function () {
            node.active = false
        })))
    }
}
